package cn.changshengjue.dakong.listener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import cn.changshengjue.dakong.main;
import cn.changshengjue.dakong.api.apiImpl;
import cn.changshengjue.dakong.bean.setting;
import cn.changshengjue.dakong.config.cfg;
import cn.changshengjue.dakong.utils.Utils;

public class ExamplesListener implements Listener {

	/**
	 * 点击打孔
	 * 
	 * @param i
	 */
	@EventHandler
	public void clickinv(InventoryClickEvent i) {
		InventoryHolder holder = i.getInventory().getHolder();
		if (i.getSlot() == -999) {
			return;
		}
		if (i.getWhoClicked().getGameMode().equals(GameMode.CREATIVE)) {
			return;
		}

		/*
		 * if (!InventoryType.CRAFTING.equals(i.getInventory().getType())) { return; }
		 */

		// 鼠标拖动的物品
		ItemStack itemOnCursor = i.getWhoClicked().getItemOnCursor();
		// 被点击的物品
		ItemStack currentItem = i.getCurrentItem();
		if (Utils.itemLoreIsNull(currentItem)) {
			return;
		}
		if (Utils.itemDisplayNameIsNull(itemOnCursor)) {
			return;
		}
		apiImpl api = new apiImpl();
		Map<String, setting> settingMap = api.getSetting();
		Set<String> keySet = settingMap.keySet();
		for (String s : keySet) {
			setting setting = settingMap.get(s);
			// 判断是不是打孔石
			if (!setting.getK_name().equals(itemOnCursor.getItemMeta().getDisplayName())) {
				continue;
			}
			Player player = Bukkit.getPlayer(i.getWhoClicked().getName());
			String replace = currentItem.toString().replace("ItemStack{", "")
					.replace("x " + currentItem.getAmount(), "").replace(" ", "").replace("}", "");
			String[] split;
			if (replace.contains(",")) {
				split = replace.split(",");
				if (!setting.getK_type().toString().equals("[]")) {
					if (!setting.getK_type().toString().contains(split[0])) {
						player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_KTYPE);
						i.setCancelled(true);
						return;
					}
				}
			} else {
				if (!setting.getK_type().toString().equals("[]")) {
					// 判断是否是可打孔类型
					if (!setting.getK_type().toString().contains(replace)) {
						player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_KTYPE);
						i.setCancelled(true);
						return;
					}
				}
			}
			// 判断是不是可打孔
			if (!currentItem.getItemMeta().getLore().toString().contains(setting.getK_lore())) {
				player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_KNULL);
				i.setCancelled(true);
				return;
			}
			// 判断是否符合装备等级
			if (api.getItemLevel(currentItem) >= Integer.valueOf(setting.getK_level_attributeLevel())) {
				i.setCancelled(true);
				player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_KLEVEL);
				return;
			}
			// 判断 活力是否足够
			boolean hasVitality = cn.changshengjue.lucky.api.LuckyApi.hasVitality(player.getName(),
					setting.getK_consume_vitality());
			if (hasVitality == false) {
				i.setCancelled(true);
				player.sendMessage(
						cfg.MSG_PLUGIN + cfg.MSG_VITALITY.replace("%vitality%", setting.getK_consume_vitality()));
				return;
			}
			// 判断 是否开第一个孔
			if (setting.isK_random_first()) {
				// 判断是否没开孔
				if (api.getYiKaiKong(setting.getK_replaceLore(), currentItem.getItemMeta().getLore()) == 0) {
					i.setCancelled(true);
					// 扣除强化石
					if (setting.isK_consume_consumeItem()) {
						if (itemOnCursor.getAmount() == 1) {
							i.getWhoClicked().setItemOnCursor(null);
						} else {
							ItemStack itemOnCursor2 = i.getWhoClicked().getItemOnCursor();
							itemOnCursor2.setAmount(itemOnCursor2.getAmount() - 1);
							i.getWhoClicked().setItemOnCursor(itemOnCursor2);
						}
					}
					// 设置lore
					ItemMeta itemMeta = currentItem.getItemMeta();
					List<String> lore = itemMeta.getLore();
					ArrayList<String> arrayList = new ArrayList<String>();
					int a = 1;
					for (String s1 : lore) {
						if (s1.equals(setting.getK_lore())) {
							if (a == 1) {
								arrayList.add(setting.getK_replaceLore());
								a++;
								continue;
							}
						}
						arrayList.add(s1);
					}
					itemMeta.setLore(arrayList);
					i.getCurrentItem().setItemMeta(itemMeta);
					// 幸运值归1
					cn.changshengjue.lucky.api.LuckyApi.setLucky(player.getName(), 0.1 + "");
					// 扣除活力
					cn.changshengjue.lucky.api.LuckyApi.takeVitality(player.getName(), setting.getK_consume_vitality());
					player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_SUCCESS);
					return;
				}
			}

			// 无损坏
			if (api.getYiKaiKong(setting.getK_replaceLore(), currentItem.getItemMeta().getLore()) <= Integer
					.valueOf(setting.getK_random_number())) {
				i.setCancelled(true);
				// 获取成功率
				double probability = api.getProbability(setting.getK_random_probability(), player);
				double randomNumberInRange = Utils.getRandomNumberInRange(0, 100);

				// 成功
				if (probability >= randomNumberInRange) {
					// 设置lore
					ItemMeta itemMeta = currentItem.getItemMeta();
					List<String> lore = itemMeta.getLore();
					ArrayList<String> arrayList = new ArrayList<String>();
					int a = 1;
					for (String s1 : lore) {
						if (s1.equals(setting.getK_lore())) {
							if (a == 1) {
								arrayList.add(setting.getK_replaceLore());
								a++;
								continue;
							}
						}
						arrayList.add(s1);
					}
					itemMeta.setLore(arrayList);
					i.getCurrentItem().setItemMeta(itemMeta);

					// 扣除强化石
					if (setting.isK_consume_consumeItem()) {
						if (itemOnCursor.getAmount() == 1) {
							i.getWhoClicked().setItemOnCursor(null);
						} else {
							ItemStack itemOnCursor2 = i.getWhoClicked().getItemOnCursor();
							itemOnCursor2.setAmount(itemOnCursor2.getAmount() - 1);
							i.getWhoClicked().setItemOnCursor(itemOnCursor2);
						}
					}
					// 幸运值归1
					cn.changshengjue.lucky.api.LuckyApi.setLucky(player.getName(), 0.1 + "");
					// 扣除活力
					cn.changshengjue.lucky.api.LuckyApi.takeVitality(player.getName(), setting.getK_consume_vitality());
					player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_SUCCESS);
					return;
				} else {
					i.setCancelled(true);
					// 增加幸运值
					cn.changshengjue.lucky.api.LuckyApi.addLucky(player.getName(),
							api.getLuckyResult(setting.getK_random_lucky()));
					// 扣除活力
					cn.changshengjue.lucky.api.LuckyApi.takeVitality(player.getName(), setting.getK_consume_vitality());
					// 扣除强化石
					if (setting.isK_consume_consumeItem()) {
						if (itemOnCursor.getAmount() == 1) {
							i.getWhoClicked().setItemOnCursor(null);
						} else {
							ItemStack itemOnCursor2 = i.getWhoClicked().getItemOnCursor();
							itemOnCursor2.setAmount(itemOnCursor2.getAmount() - 1);
							i.getWhoClicked().setItemOnCursor(itemOnCursor2);
						}
					}
					player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_FAIL);

				}

			} else {
				i.setCancelled(true);
				// 损坏
				// 获取成功率
				double probability = api.getProbability(setting.getK_random_probability(), player);
				double randomNumberInRange = Utils.getRandomNumberInRange(0, 100);
				// 损坏后判断是否开启防爆
				if (api.isFB(player.getName())) {
					// 判断背包防爆是否足够
					if (api.isFBNumber(setting.getK_random_noDamage(), player)) {

						ItemStack fb = api.getFB(setting.getK_random_noDamage());
						fb.setAmount(setting.getK_random_noDamageNum());
						player.getInventory().removeItem(fb);
						// 成功
						if (probability >= randomNumberInRange) {
							// 设置lore
							ItemMeta itemMeta = currentItem.getItemMeta();
							List<String> lore = itemMeta.getLore();
							ArrayList<String> arrayList = new ArrayList<String>();
							int a = 1;
							for (String s1 : lore) {
								if (s1.equals(setting.getK_lore())) {
									if (a == 1) {
										arrayList.add(setting.getK_replaceLore());
										a++;
										continue;
									}
								}
								arrayList.add(s1);
							}
							itemMeta.setLore(arrayList);
							i.getCurrentItem().setItemMeta(itemMeta);

							// 扣除强化石
							if (setting.isK_consume_consumeItem()) {
								if (itemOnCursor.getAmount() == 1) {
									i.getWhoClicked().setItemOnCursor(null);
								} else {
									ItemStack itemOnCursor2 = i.getWhoClicked().getItemOnCursor();
									itemOnCursor2.setAmount(itemOnCursor2.getAmount() - 1);
									i.getWhoClicked().setItemOnCursor(itemOnCursor2);
								}
							}
							// 幸运值归1
							cn.changshengjue.lucky.api.LuckyApi.setLucky(player.getName(), 0.1 + "");
							// 扣除活力
							cn.changshengjue.lucky.api.LuckyApi.takeVitality(player.getName(),
									setting.getK_consume_vitality());
							player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_TAKEFB_1);
							return;
						} else {
							i.setCancelled(true);
							// 幸运值归1
							// cn.changshengjue.lucky.api.LuckyApi.setLucky(player.getName(), 0.1 + "");
							// 增加幸运值
							cn.changshengjue.lucky.api.LuckyApi.addLucky(player.getName(),
									api.getLuckyResult(setting.getK_random_lucky()));
							// api.getLuckyResult(setting.getK_random_lucky()));
							// 扣除活力
							cn.changshengjue.lucky.api.LuckyApi.takeVitality(player.getName(),
									setting.getK_consume_vitality());
							// 扣除强化石
							if (setting.isK_consume_consumeItem()) {
								if (itemOnCursor.getAmount() == 1) {
									i.getWhoClicked().setItemOnCursor(null);
								} else {
									ItemStack itemOnCursor2 = i.getWhoClicked().getItemOnCursor();
									itemOnCursor2.setAmount(itemOnCursor2.getAmount() - 1);
									i.getWhoClicked().setItemOnCursor(itemOnCursor2);
								}
							}

							player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_TAKEFB);
							return;
						}
					} else {
						player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_FB);
					}
				} else {
					player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_NODAMAGEON);
				}
				// 成功
				if (probability >= randomNumberInRange) {
					// 设置lore
					ItemMeta itemMeta = currentItem.getItemMeta();
					List<String> lore = itemMeta.getLore();
					ArrayList<String> arrayList = new ArrayList<String>();
					int a = 1;
					for (String s1 : lore) {
						if (s1.equals(setting.getK_lore())) {
							if (a == 1) {
								arrayList.add(setting.getK_replaceLore());
								a++;
								continue;
							}
						}
						arrayList.add(s1);
					}
					itemMeta.setLore(arrayList);
					i.getCurrentItem().setItemMeta(itemMeta);

					// 扣除强化石
					if (setting.isK_consume_consumeItem()) {
						if (itemOnCursor.getAmount() == 1) {
							i.getWhoClicked().setItemOnCursor(null);
						} else {
							ItemStack itemOnCursor2 = i.getWhoClicked().getItemOnCursor();
							itemOnCursor2.setAmount(itemOnCursor2.getAmount() - 1);
							i.getWhoClicked().setItemOnCursor(itemOnCursor2);
						}
					}
					// 幸运值归1
					cn.changshengjue.lucky.api.LuckyApi.setLucky(player.getName(), 0.1 + "");
					// 扣除活力
					cn.changshengjue.lucky.api.LuckyApi.takeVitality(player.getName(), setting.getK_consume_vitality());
					player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_SUCCESS);
					return;
				} else {
					i.setCancelled(true);
					// 判断 是否损坏
					double damageProbability = api.getDamageProbability(setting.getK_random_damageProbability(),
							player);
					double randomNumber = Utils.getRandomNumberInRange(0, 100);
					// 损坏
					if (damageProbability < randomNumber) {
						// 设置lore
						ItemMeta itemMeta = currentItem.getItemMeta();
						List<String> lore = itemMeta.getLore();
						ArrayList<String> arrayList = new ArrayList<String>();
						int a = 1;
						for (String s1 : lore) {
							if (s1.equals(setting.getK_lore())) {
								if (a == 1) {
									a++;
									continue;
								}
							}
							arrayList.add(s1);
						}
						itemMeta.setLore(arrayList);
						i.getCurrentItem().setItemMeta(itemMeta);

						// 增加幸运值
						cn.changshengjue.lucky.api.LuckyApi.addLucky(player.getName(),
								api.getLuckyResult(setting.getK_random_lucky()));
						// 扣除活力
						cn.changshengjue.lucky.api.LuckyApi.takeVitality(player.getName(),
								setting.getK_consume_vitality());
						// 扣除强化石
						if (setting.isK_consume_consumeItem()) {
							if (itemOnCursor.getAmount() == 1) {
								i.getWhoClicked().setItemOnCursor(null);
							} else {
								ItemStack itemOnCursor2 = i.getWhoClicked().getItemOnCursor();
								itemOnCursor2.setAmount(itemOnCursor2.getAmount() - 1);
								i.getWhoClicked().setItemOnCursor(itemOnCursor2);
							}
						}
						player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_FAIL_1);
						return;
					} else {
						i.setCancelled(true);
						// 增加幸运值
						cn.changshengjue.lucky.api.LuckyApi.addLucky(player.getName(),
								api.getLuckyResult(setting.getK_random_lucky()));
						// 扣除活力
						cn.changshengjue.lucky.api.LuckyApi.takeVitality(player.getName(),
								setting.getK_consume_vitality());
						// 扣除强化石
						if (setting.isK_consume_consumeItem()) {
							if (itemOnCursor.getAmount() == 1) {
								i.getWhoClicked().setItemOnCursor(null);
							} else {
								ItemStack itemOnCursor2 = i.getWhoClicked().getItemOnCursor();
								itemOnCursor2.setAmount(itemOnCursor2.getAmount() - 1);
								i.getWhoClicked().setItemOnCursor(itemOnCursor2);
							}
						}
						player.sendMessage(cfg.MSG_PLUGIN + cfg.MSG_FAIL);
						return;
					}

				}
			}
		}
	}
}
